|
|
|
|
|
|
|
Figure T4-3a,
T4-3b: Passing a single parameter to a function |
|
|
|
|
|
|
|
|
When a function has multiple parameters, they are placed on the stack in order (more on this later). Even though the processor can only add and remove values at the top of the stack, it is able to read and write values anywhere on the stack. This is not surprising when you remember that the stack is really nothing more than a memory buffer. |
|
|
|
|
|
|
|
|
Let's review for a moment. You've seen that the stack is essential for storing return addresses for function calls. Now you can see that it is also used for passing parameters to functions where each function has its own copy of each parameter. One can't help but ask: if the stack can hold a unique set of parameter values for each function call, could it also be used to hold other variables that are unique to each function call? |
|
|
|
|
|
|
|
|
The answer is yes, and that is exactly how local variables are handled. |
|
|
|
|
|
|
|
|
Let's take another look at Function B(). The version that follows works exactly the same way as the previous one except that it uses a local variable to hold an intermediate value which, in this case, is set to the same value as parameter L. |
|
|
|
|
|
|
|
|
Function B(ByVal L As Long) As Long
Debug.Print L
Dim LocalVar As Long |
|
|
|
|
|